package com.jndv.phoneconfig.websocket;

import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jndv.phoneconfig.PublicConstant;
import com.jndv.phoneconfig.command.AutoTimeStateCommand;
import com.jndv.phoneconfig.command.BatteryCommand;
import com.jndv.phoneconfig.command.BluetoothStateCommand;
import com.jndv.phoneconfig.command.DeviceCommand;
import com.jndv.phoneconfig.command.GpsStateCommand;
import com.jndv.phoneconfig.command.HotspotStateCommand;
import com.jndv.phoneconfig.command.Hour24FormatCommand;
import com.jndv.phoneconfig.command.MobileDataStateCommand;
import com.jndv.phoneconfig.command.MusicVolumeCommand;
import com.jndv.phoneconfig.command.PairResult;

import org.greenrobot.eventbus.EventBus;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.Map;

public class JWebSocketClient extends WebSocketClient {

    Context mContext;

    public JWebSocketClient(Context context,URI serverUri) {
        super(serverUri, new Draft_6455());
        mContext=context;
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
    }

    @Override
    public void onMessage(String message) {
        Log.e("zhang", "收到消息::"+message);
        Gson gson=new Gson();
        Map<String, Object> jsonMap = gson.fromJson(message, new TypeToken<Map<String, Object>>() { }.getType());
        if(jsonMap.containsKey(PublicConstant.BATTERY)){
            String battery = (String) jsonMap.get(PublicConstant.BATTERY);
            BatteryCommand command=new BatteryCommand(battery);
            EventBus.getDefault().post(command);
        } else if(jsonMap.containsKey(PublicConstant.DEVICE_NAME)){
            String name = (String) jsonMap.get(PublicConstant.DEVICE_NAME);
            DeviceCommand command=new DeviceCommand(name);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.PAIR_RESULT)){
            String value= (String) jsonMap.get(PublicConstant.PAIR_RESULT);
            PairResult pairResult=new Gson().fromJson(value,PairResult.class);
            EventBus.getDefault().post(pairResult);
        }else if(jsonMap.containsKey(PublicConstant.CUR_HOTSPOT_STATE)){
            boolean value= (boolean) jsonMap.get(PublicConstant.CUR_HOTSPOT_STATE);
            HotspotStateCommand command=new HotspotStateCommand(value);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.CUR_MOBILE_DATA_STATE)){
            boolean value= (Boolean) jsonMap.get(PublicConstant.CUR_MOBILE_DATA_STATE);
            MobileDataStateCommand command=new MobileDataStateCommand(value);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.CUR_BLUETOOTH_STATE)){
            boolean value= (Boolean) jsonMap.get(PublicConstant.CUR_BLUETOOTH_STATE);
            BluetoothStateCommand command=new BluetoothStateCommand(value);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.CUR_GPS_STATE)){
            boolean value= (Boolean) jsonMap.get(PublicConstant.CUR_GPS_STATE);
            GpsStateCommand command=new GpsStateCommand(value);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.CUR_MUSIC_VOLUME)){
            String value= (String) jsonMap.get(PublicConstant.CUR_MUSIC_VOLUME);
            MusicVolumeCommand command=new Gson().fromJson(value,MusicVolumeCommand.class);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.CUR_24_HOUR_STATE)){
            boolean value= (Boolean) jsonMap.get(PublicConstant.CUR_24_HOUR_STATE);
            Hour24FormatCommand command=new Hour24FormatCommand(value);
            EventBus.getDefault().post(command);
        }else if(jsonMap.containsKey(PublicConstant.CUR_AUTO_TIME_STATE)){
            boolean value= (Boolean) jsonMap.get(PublicConstant.CUR_AUTO_TIME_STATE);
            AutoTimeStateCommand command=new AutoTimeStateCommand(value);
            EventBus.getDefault().post(command);
        }
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        Log.d("zhang","onClose...reason==="+reason);
    }

    @Override
    public void onError(Exception ex) {
        Log.d("zhang","onError...Exception==="+ex);
    }
}
